package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import dao.interfaces.IRatingDAO;
import model.Rating;

public class RatingDAO extends DAO<Rating> implements IRatingDAO<Rating> {
	
	public RatingDAO(Connection conn) {
		super(conn);
	}
	
	/**
	 * implements the inherited abstract methods from the DAO<User> abstract class
	 */
	
	@Override
	public boolean insert(Rating aRating) {
		String sql =
			"INSERT INTO cook_rating " +
			"(rating_id, rating_value, recipe_id) " +
			"VALUES (" + aRating.getId()  + ", " + aRating.getValue()+ ", " + aRating.getRecipe_id() + ")";
		try {
			Statement stm = connection.createStatement();
			stm.executeUpdate(sql);
			stm.close();
			return true;
			} 
		catch (SQLException e) {
			e.printStackTrace();
			return false;
		}	
	}

	@Override
	public boolean delete(Rating obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Rating obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Rating findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}	

	@Override
	public List<Rating> getAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * implements the inherited abstract methods from the IRatingDAO<Rating> interface
	 */
	
	public List<Rating> getListBy(int recipe_id) {
		List<Rating> ratingList = new ArrayList<Rating>();
		String sql =
			"SELECT * " +
			"FROM cook_rating " +
			"WHERE recipe_id = " + recipe_id ;
		
		try {
			Statement stm = connection.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			
			while (rs.next()) {
				Rating rating = new Rating();
				rating.setId(rs.getInt("rating_id"));
				rating.setValue(rs.getInt("rating_value"));
				rating.setRecipe_id(rs.getInt("recipe_id"));
				ratingList.add(rating);
			}
			if (ratingList.size() > 0)
				return ratingList;
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		return null;
	}

}
